package com.han.utils;



import lombok.extern.slf4j.Slf4j;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

@Slf4j
public class JavaEmail {


    public static  void sendEmail(String myEmail,String ToEmail,String host){


        Properties properties=System.getProperties();

        properties.setProperty("mail.smtp.host",host);

        Session session=Session.getDefaultInstance(properties);


        try {

            //创建默认的MimeMessage对象
            MimeMessage mimeMessage=new MimeMessage(session);

            //Set From ：头部头字段
            mimeMessage.setFrom(new InternetAddress(myEmail));

            //Set To :头部头字段
            mimeMessage.addRecipients(Message.RecipientType.TO, String.valueOf(new InternetAddress(ToEmail)));

            mimeMessage.setText("This id actual message");

            //发送消息
            Transport.send(mimeMessage);

            log.info("消息发送成功");

        } catch (AddressException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        }


    }
}
